util.AddNetworkString( "KKB_UserGroups_New" )
net.Receive( "KKB_UserGroups_New", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Group = net.ReadString()
	if ( KKB.UserGroups[Group] == nil ) then
		KKB.UserGroups[Group] = {}
		KKB.UserGroups[Group].Spawn = {}
		KKB.UserGroups[Group].Spawn.Vehicles = false
		KKB.UserGroups[Group].Spawn.Weapons = false
		KKB.UserGroups[Group].Spawn.NPCs = false
		KKB.UserGroups[Group].Spawn.Sents = false
		KKB.UserGroups[Group].Spawn.Other = false
		KKB.UserGroups[Group].Physgun = {}
		KKB.UserGroups[Group].Physgun.World = false
		KKB.UserGroups[Group].Physgun.WorldProps = false
		KKB.UserGroups[Group].Physgun.Vehicles = false
		KKB.UserGroups[Group].Physgun.Weapons = false
		KKB.UserGroups[Group].Physgun.NPCs = false
		KKB.UserGroups[Group].Physgun.Sents = false
		KKB.UserGroups[Group].Physgun.Other = false
		KKB.UserGroups[Group].Toolgun = {}
		KKB.UserGroups[Group].Toolgun.World = false
		KKB.UserGroups[Group].Toolgun.WorldProps = false
		KKB.UserGroups[Group].Toolgun.Vehicles = false
		KKB.UserGroups[Group].Toolgun.Weapons = false
		KKB.UserGroups[Group].Toolgun.NPCs = false
		KKB.UserGroups[Group].Toolgun.Sents = false
		KKB.UserGroups[Group].Toolgun.Other = false
		KKB.UserGroups[Group].Gravcannon = {}
		KKB.UserGroups[Group].Gravcannon.World = false
		KKB.UserGroups[Group].Gravcannon.WorldProps = false
		KKB.UserGroups[Group].Gravcannon.Vehicles = false
		KKB.UserGroups[Group].Gravcannon.Weapons = false
		KKB.UserGroups[Group].Gravcannon.NPCs = false
		KKB.UserGroups[Group].Gravcannon.Sents = false
		KKB.UserGroups[Group].Gravcannon.Other = false
		KKB.UserGroups[Group].Damage = {}
		KKB.UserGroups[Group].Damage.World = false
		KKB.UserGroups[Group].Damage.WorldProps = false
		KKB.UserGroups[Group].Damage.Vehicles = false
		KKB.UserGroups[Group].Damage.Weapons = false
		KKB.UserGroups[Group].Damage.NPCs = false
		KKB.UserGroups[Group].Damage.Sents = false
		KKB.UserGroups[Group].Damage.Players = false
		KKB.UserGroups[Group].Damage.Other = false
		KKB.UserGroups[Group].Use = {}
		KKB.UserGroups[Group].Use.World = false
		KKB.UserGroups[Group].Use.WorldProps = false
		KKB.UserGroups[Group].Use.Vehicles = false
		KKB.UserGroups[Group].Use.Weapons = false
		KKB.UserGroups[Group].Use.NPCs = false
		KKB.UserGroups[Group].Use.Sents = false
		KKB.UserGroups[Group].Use.Other = false
		KKB.UserGroups[Group].Other = {}
		KKB.UserGroups[Group].Other.EnterVehicles = false
		KKB.UserGroups[Group].Other.PickupWeapons = false
		KKB.UserGroups[Group].Other.ContextOptions = false
		KKB.UserGroups[Group].Other.IgnoreBuddys = false
		KKB.UserGroups[Group].Other.AllToolguns = false
		KKB.UserGroups[Group].Donate = {}
		KKB.UserGroups[Group].Donate.Enabled = false
		KKB.UserGroups[Group].Donate.Amount = 0
		KKB.UserGroups[Group].Donate.Lump = false
		KKB.Send_UserGroups( nil )
		KKB.Save_UserGroup( Group )
	end
end)

util.AddNetworkString( "KKB_UserGroups_Delete" )
net.Receive( "KKB_UserGroups_Delete", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Groups = net.ReadTable()
	local UserGroups = {}
	for k,_ in pairs(KKB.UserGroups) do
		if ( not table.HasValue(Groups,k) ) then
			UserGroups[k] = KKB.UserGroups[k]
		end
	end
	KKB.UserGroups = UserGroups
	KKB.Send_UserGroups( nil )
end)

util.AddNetworkString( "KKB_UserGroups_Change" )
net.Receive( "KKB_UserGroups_Change", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Groups = net.ReadTable()
	local Key = net.ReadString()
	local Option = net.ReadString()
	local Value = KKB.IntToBool(tonumber(net.ReadUInt( 32 )))
	for _,v in pairs(Groups) do
		if ( KKB.UserGroups[v][Key][Option] != Value ) then
			KKB.UserGroups[v][Key][Option] = Value
			KKB.Save_UserGroup( v )
		end
	end
	KKB.Send_UserGroups( nil )
end)

util.AddNetworkString( "KKB_CustomUsers_Add" )
net.Receive( "KKB_CustomUsers_Add", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Group = net.ReadString()
	local UniqueID = net.ReadString()
	local Ply = player.GetByUniqueID( UniqueID )
	if ( KKB.CustomUsers[UniqueID] == nil ) then
		local CurrentGroup = Ply:GetUserGroup()
		if ( CurrentGroup != Group ) then
			KKB.CustomUsers[UniqueID] = {}
			KKB.CustomUsers[UniqueID].Nick = Ply:Nick()
			KKB.CustomUsers[UniqueID].Group = Group
			KKB.Send_CustomUsers( nil )
			KKB.Save_CustomUser( UniqueID )
		end
	end
end)

util.AddNetworkString( "KKB_CustomUsers_Change" )
net.Receive( "KKB_CustomUsers_Change", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Group = net.ReadString()
	local UniqueID = net.ReadString()
	if ( KKB.CustomUsers[UniqueID] != nil ) then
		local Ply = player.GetByUniqueID( UniqueID )
		if ( IsValid(Ply) ) then
			local CurrentGroup = Ply:GetUserGroup()
			if ( CurrentGroup != Group ) then
				KKB.CustomUsers[UniqueID].Group = Group
				KKB.CustomUsers[UniqueID].Nick = Ply:Nick()
			else
				local CustomUsers = {}
				for k,_ in pairs(KKB.CustomUsers) do
					if ( k != UniqueID ) then
						CustomUsers[k] = KKB.CustomUsers[k]
					end
				end
				KKB.CustomUsers = CustomUsers
			end
			KKB.Send_CustomUsers( nil )
			KKB.Save_CustomUser( UniqueID )
		else
			print("PLY NOT VALID")
			--Move player to new group anyway
			--Make player initial spawn hook to
			--Loop through cusom users
			--Checking Group against UCL Group
			--If match delete custom user entry
		end
	end
end)

util.AddNetworkString( "KKB_CustomUsers_Delete" )
net.Receive( "KKB_CustomUsers_Delete", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local UniqueID = net.ReadString()
	if ( KKB.CustomUsers[UniqueID] != nil ) then
		local CustomUsers = {}
		for k,_ in pairs(KKB.CustomUsers) do
			if ( k != UniqueID ) then
				CustomUsers[k] = KKB.CustomUsers[k]
			end
		end
		KKB.CustomUsers = CustomUsers
		KKB.Send_CustomUsers( nil )
		KKB.Save_CustomUser( UniqueID )
	end
end)

util.AddNetworkString( "KKB_Damage_Change" )
net.Receive( "KKB_Damage_Change", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Type = net.ReadString()
	local Val = KKB.IntToBool(net.ReadUInt(32))
	if ( KKB.Damage[Type] != Val ) then
		KKB.Damage[Type] = Val
		KKB.Send_Damage( nil )
		KKB.Save_Damage()
	end
end)

util.AddNetworkString( "KKB_Settings_Update" )
net.Receive( "KKB_Settings_Update", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Type1 = net.ReadString()
	local Type2 = net.ReadString()
	local NumVal = net.ReadUInt( 32 )
	local Val = KKB.IntToBool(NumVal)
	local Table = KKB.Settings[Type1]
	if ( Table.Blacklist != Val ) then
		Table.Blacklist = Val
		KKB.Send_Settings( nil )
		KKB.Save_Settings()
	end
end)

util.AddNetworkString( "KKB_CleanupTime_Change" )
net.Receive( "KKB_CleanupTime_Change", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Seconds = net.ReadUInt( 32 )
	if ( KKB.Settings.Server.CleanupTime != Seconds ) then
		KKB.Settings.Server.CleanupTime = Seconds
		KKB.Send_Settings( nil )
		KKB.Save_Settings()
	end
end)

util.AddNetworkString( "KKB_SpawnTime_Change" )
net.Receive( "KKB_SpawnTime_Change", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Seconds = net.ReadUInt( 32 )
	if ( KKB.Settings.Server.SpawnTime != Seconds ) then
		KKB.Settings.Server.SpawnTime = Seconds
		KKB.Send_Settings( nil )
		KKB.Save_Settings()
	end
end)

util.AddNetworkString( "KKB_BlockedModels_Add" )
net.Receive( "KKB_BlockedModels_Add", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Model = net.ReadString()
	if ( not table.HasValue(KKB.BlockedModels, Model) ) then
		table.insert(KKB.BlockedModels, Model)
		KKB.Send_BlockedModels( nil )
		KKB.Save_Model( Model )
	end
end)

util.AddNetworkString( "KKB_BlockedModels_Delete" )
net.Receive( "KKB_BlockedModels_Delete", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Models = net.ReadTable()
	local BlockedModels = {}
	for _,v in pairs(KKB.BlockedModels) do
		if ( not table.HasValue(Models, v) ) then
			table.insert(BlockedModels, v)
			KKB.Save_Model( v )
		end
	end
	KKB.BlockedModels = BlockedModels
	KKB.Send_BlockedModels( nil )
end)

util.AddNetworkString( "KKB_Entity_Add" )
net.Receive( "KKB_Entity_Add", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Groups = net.ReadTable()
	local Entity = net.ReadString()
	if ( KKB.EntityRestrictions[Entity] == nil ) then
		KKB.EntityRestrictions[Entity] = {}
		KKB.EntityRestrictions[Entity].Groups = Groups
		KKB.EntityRestrictions[Entity].Options = {}
		KKB.EntityRestrictions[Entity].Options.Physgun = false
		KKB.EntityRestrictions[Entity].Options.Toolgun = false
		KKB.EntityRestrictions[Entity].Options.Gravcannon = false
		KKB.EntityRestrictions[Entity].Options.Use = false
		KKB.EntityRestrictions[Entity].Options.Damage = false
	else
		for _,v in pairs(Groups) do
			if ( not table.HasValue(KKB.EntityRestrictions[Entity].Groups, v) ) then
				table.insert(KKB.EntityRestrictions[Entity].Groups, v)
			end
		end
	end
	KKB.Send_EntityRestrictions( nil )
	KKB.Save_Entity( Entity )
end)

util.AddNetworkString( "KKB_Entity_Delete" )
net.Receive( "KKB_Entity_Delete", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Groups = net.ReadTable()
	local Entity = net.ReadString()
	if ( KKB.EntityRestrictions[Entity] != nil ) then
		local NewGroups = {}
		for _,v in pairs(KKB.EntityRestrictions[Entity].Groups) do
			if ( not table.HasValue(Groups, v) ) then
				table.insert(NewGroups, v)
			end
		end
		KKB.EntityRestrictions[Entity].Groups = NewGroups
		KKB.Send_EntityRestrictions( nil )
		KKB.Save_Entity( Entity )
	end
end)

util.AddNetworkString( "KKB_Entity_Update" )
net.Receive( "KKB_Entity_Update", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Entity = net.ReadString()
	local Type = net.ReadString()
	local Val = KKB.IntToBool(net.ReadUInt(32))
	if ( KKB.EntityRestrictions[Entity] != nil ) then
		if ( KKB.EntityRestrictions[Entity].Options[Type] != Val ) then
			KKB.EntityRestrictions[Entity].Options[Type] = Val
		end
		KKB.Send_EntityRestrictions( nil )
		KKB.Save_Entity( Entity )
	end
end)

util.AddNetworkString( "KKB_NPC_Add" )
net.Receive( "KKB_NPC_Add", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Groups = net.ReadTable()
	local NPC = net.ReadString()
	if ( KKB.NPCRestrictions[NPC] == nil ) then
		KKB.NPCRestrictions[NPC] = {}
		KKB.NPCRestrictions[NPC].Groups = Groups
		KKB.NPCRestrictions[NPC].Options = {}
		KKB.NPCRestrictions[NPC].Options.Physgun = false
		KKB.NPCRestrictions[NPC].Options.Toolgun = false
		KKB.NPCRestrictions[NPC].Options.Gravcannon = false
		KKB.NPCRestrictions[NPC].Options.Use = false
		KKB.NPCRestrictions[NPC].Options.Damage = false
	else
		for _,v in pairs(Groups) do
			if ( not table.HasValue(KKB.NPCRestrictions[NPC].Groups, v) ) then
				table.insert(KKB.NPCRestrictions[NPC].Groups, v)
			end
		end
	end
	KKB.Send_NPCRestrictions( nil )
	KKB.Save_NPC( NPC )
end)

util.AddNetworkString( "KKB_NPC_Delete" )
net.Receive( "KKB_NPC_Delete", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Groups = net.ReadTable()
	local NPC = net.ReadString()
	if ( KKB.NPCRestrictions[NPC] != nil ) then
		local NewGroups = {}
		for _,v in pairs(KKB.NPCRestrictions[NPC].Groups) do
			if ( not table.HasValue(Groups, v) ) then
				table.insert(NewGroups, v)
			end
		end
		KKB.NPCRestrictions[NPC].Groups = NewGroups
		KKB.Send_NPCRestrictions( nil )
		KKB.Save_NPC( NPC )
	end
end)

util.AddNetworkString( "KKB_NPC_Update" )
net.Receive( "KKB_NPC_Update", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local NPC = net.ReadString()
	local Type = net.ReadString()
	local Val = KKB.IntToBool(net.ReadUInt(32))
	if ( KKB.NPCRestrictions[NPC] != nil ) then
		if ( KKB.NPCRestrictions[NPC].Options[Type] != Val ) then
			KKB.NPCRestrictions[NPC].Options[Type] = Val
		end
		KKB.Send_NPCRestrictions( nil )
		KKB.Save_NPC( NPC )
	end
end)

util.AddNetworkString( "KKB_Vehicle_Add" )
net.Receive( "KKB_Vehicle_Add", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Groups = net.ReadTable()
	local Vehicle = net.ReadString()
	if ( KKB.VehicleRestrictions[Vehicle] == nil ) then
		KKB.VehicleRestrictions[Vehicle] = {}
		KKB.VehicleRestrictions[Vehicle].Groups = Groups
		KKB.VehicleRestrictions[Vehicle].Options = {}
		KKB.VehicleRestrictions[Vehicle].Options.Physgun = false
		KKB.VehicleRestrictions[Vehicle].Options.Toolgun = false
		KKB.VehicleRestrictions[Vehicle].Options.Gravcannon = false
		KKB.VehicleRestrictions[Vehicle].Options.Use = false
		KKB.VehicleRestrictions[Vehicle].Options.Damage = false
	else
		for _,v in pairs(Groups) do
			if ( not table.HasValue(KKB.VehicleRestrictions[Vehicle].Groups, v) ) then
				table.insert(KKB.VehicleRestrictions[Vehicle].Groups, v)
			end
		end
	end
	KKB.Send_VehicleRestrictions( nil )
	KKB.Save_Vehicle( Vehicle )
end)

util.AddNetworkString( "KKB_Vehicle_Delete" )
net.Receive( "KKB_Vehicle_Delete", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Groups = net.ReadTable()
	local Vehicle = net.ReadString()
	if ( KKB.VehicleRestrictions[Vehicle] != nil ) then
		local NewGroups = {}
		for _,v in pairs(KKB.VehicleRestrictions[Vehicle].Groups) do
			if ( not table.HasValue(Groups, v) ) then
				table.insert(NewGroups, v)
			end
		end
		KKB.VehicleRestrictions[Vehicle].Groups = NewGroups
		KKB.Send_VehicleRestrictions( nil )
		KKB.Save_Vehicle( Vehicle )
	end
end)

util.AddNetworkString( "KKB_Vehicle_Update" )
net.Receive( "KKB_Vehicle_Update", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Vehicle = net.ReadString()
	local Type = net.ReadString()
	local Val = KKB.IntToBool(net.ReadUInt(32))
	if ( KKB.VehicleRestrictions[Vehicle] != nil ) then
		if ( KKB.VehicleRestrictions[Vehicle].Options[Type] != Val ) then
			KKB.VehicleRestrictions[Vehicle].Options[Type] = Val
		end
		KKB.Send_VehicleRestrictions( nil )
		KKB.Save_Vehicle( Vehicle )
	end
end)

util.AddNetworkString( "KKB_Weapon_Add" )
net.Receive( "KKB_Weapon_Add", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Groups = net.ReadTable()
	local Weapon = net.ReadString()
	if ( KKB.WeaponRestrictions[Weapon] == nil ) then
		KKB.WeaponRestrictions[Weapon] = {}
		KKB.WeaponRestrictions[Weapon].Groups = Groups
		KKB.WeaponRestrictions[Weapon].Options = {}
		KKB.WeaponRestrictions[Weapon].Options.Physgun = false
		KKB.WeaponRestrictions[Weapon].Options.Toolgun = false
		KKB.WeaponRestrictions[Weapon].Options.Gravcannon = false
		KKB.WeaponRestrictions[Weapon].Options.Use = false
		KKB.WeaponRestrictions[Weapon].Options.Damage = false
	else
		for _,v in pairs(Groups) do
			if ( not table.HasValue(KKB.WeaponRestrictions[Weapon].Groups, v) ) then
				table.insert(KKB.WeaponRestrictions[Weapon].Groups, v)
			end
		end
	end
	KKB.Send_WeaponRestrictions( nil )
	KKB.Save_Weapon( Weapon )
end)

util.AddNetworkString( "KKB_Weapon_Delete" )
net.Receive( "KKB_Weapon_Delete", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Groups = net.ReadTable()
	local Weapon = net.ReadString()
	if ( KKB.WeaponRestrictions[Weapon] != nil ) then
		local NewGroups = {}
		for _,v in pairs(KKB.WeaponRestrictions[Weapon].Groups) do
			if ( not table.HasValue(Groups, v) ) then
				table.insert(NewGroups, v)
			end
		end
		KKB.WeaponRestrictions[Weapon].Groups = NewGroups
		KKB.Send_WeaponRestrictions( nil )
		KKB.Save_Weapon( Weapon )
	end
end)

util.AddNetworkString( "KKB_Weapon_Update" )
net.Receive( "KKB_Weapon_Update", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Weapon = net.ReadString()
	local Type = net.ReadString()
	local Val = KKB.IntToBool(net.ReadUInt(32))
	if ( KKB.WeaponRestrictions[Weapon] != nil ) then
		if ( KKB.WeaponRestrictions[Weapon].Options[Type] != Val ) then
			KKB.WeaponRestrictions[Weapon].Options[Type] = Val
		end
		KKB.Send_WeaponRestrictions( nil )
		KKB.Save_Weapon( Weapon )
	end
end)

util.AddNetworkString( "KKB_Tool_Add" )
net.Receive( "KKB_Tool_Add", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Groups = net.ReadTable()
	local Toolgun = net.ReadString()
	if ( KKB.ToolgunRestrictions[Toolgun] == nil ) then
		KKB.ToolgunRestrictions[Toolgun] = {}
		KKB.ToolgunRestrictions[Toolgun].Groups = Groups
		KKB.ToolgunRestrictions[Toolgun].Options = {}
		KKB.ToolgunRestrictions[Toolgun].Options.World = false
		KKB.ToolgunRestrictions[Toolgun].Options.WorldProps = false
		KKB.ToolgunRestrictions[Toolgun].Options.Vehicles = false
		KKB.ToolgunRestrictions[Toolgun].Options.Weapons = false
		KKB.ToolgunRestrictions[Toolgun].Options.NPCs = false
		KKB.ToolgunRestrictions[Toolgun].Options.Sents = false
		KKB.ToolgunRestrictions[Toolgun].Options.Players = false
		KKB.ToolgunRestrictions[Toolgun].Options.Other = false
	else
		for _,v in pairs(Groups) do
			if ( not table.HasValue(KKB.ToolgunRestrictions[Toolgun].Groups, v) ) then
				table.insert(KKB.ToolgunRestrictions[Toolgun].Groups, v)
			end
		end
	end
	KKB.Send_ToolgunRestrictions( nil )
	KKB.Save_Tool( Toolgun )
end)

util.AddNetworkString( "KKB_Tool_Delete" )
net.Receive( "KKB_Tool_Delete", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Groups = net.ReadTable()
	local Toolgun = net.ReadString()
	if ( KKB.ToolgunRestrictions[Toolgun] != nil ) then
		local NewGroups = {}
		for _,v in pairs(KKB.ToolgunRestrictions[Toolgun].Groups) do
			if ( not table.HasValue(Groups, v) ) then
				table.insert(NewGroups, v)
			end
		end
		KKB.ToolgunRestrictions[Toolgun].Groups = NewGroups
		KKB.Send_ToolgunRestrictions( nil )
		KKB.Save_Tool( Toolgun )
	end
end)

util.AddNetworkString( "KKB_Tool_Update" )
net.Receive( "KKB_Tool_Update", function(len, ply)
	if ( not ply:IsSuperAdmin() ) then return end
	local Toolgun = net.ReadString()
	local Type = net.ReadString()
	local Val = KKB.IntToBool(net.ReadUInt(32))
	if ( KKB.ToolgunRestrictions[Toolgun] != nil ) then
		if ( KKB.ToolgunRestrictions[Toolgun].Options[Type] != Val ) then
			KKB.ToolgunRestrictions[Toolgun].Options[Type] = Val
		end
		KKB.Send_ToolgunRestrictions( nil )
		KKB.Save_Tool( Toolgun )
	end
end)

